package adapletplugin.editors;

import objects.ReferenceType;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;

public class RefTypeEditor extends AbstractSubEditor
{
	private Text fTypeText;
	private Text fTableText;
	private Text fIdText;
	private Text fOidText;
	private Text fNameText;
	private Text fRevText;
	private Text fVaultText;
	private ReferenceType focusRefType;

	public RefTypeEditor(ScrolledComposite composite, FormToolkit toolkit, MapFileEditor parentEditor)
	{
		super(composite, toolkit, parentEditor);
	}

	@Override
	protected void create()
	{
		Composite row = fToolkit.createComposite(fSubComposite);
		row.setLayout(new GridLayout(2, false));
		row.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		fToolkit.createLabel(row, "Type Name:");
		fTypeText = fToolkit.createText(row, "", SWT.BORDER | SWT.READ_ONLY);
		fTypeText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fTypeText.addModifyListener(createModifyListener_Type());

		fToolkit.createLabel(row, "Table Name:");
		fTableText = fToolkit.createText(row, "", SWT.BORDER);
		fTableText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fTableText.addModifyListener(createModifyListener_Table());

		fToolkit.createLabel(row, "ID Column:");
		fIdText = fToolkit.createText(row, "", SWT.BORDER);
		fIdText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fIdText.addModifyListener(createModifyListener_ID());

		fToolkit.createLabel(row, "OID Column:");
		fOidText = fToolkit.createText(row, "", SWT.BORDER);
		fOidText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fOidText.addModifyListener(createModifyListener_OID());

		fToolkit.createLabel(row, "Name Column:");
		fNameText = fToolkit.createText(row, "", SWT.BORDER);
		fNameText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fNameText.addModifyListener(createModifyListener_Name());

		fToolkit.createLabel(row, "Revision Column:");
		fRevText = fToolkit.createText(row, "", SWT.BORDER);
		fRevText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fRevText.addModifyListener(createModifyListener_Rev());

		fToolkit.createLabel(row, "Vault:");
		fVaultText = fToolkit.createText(row, "", SWT.BORDER);
		fVaultText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		fVaultText.addModifyListener(createModifyListener_Vault());

	}

	private ModifyListener createModifyListener_Type()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				focusRefType.setTypeName(fTypeText.getText());
				fEditor.refresh();
			}
		};
	}

	private ModifyListener createModifyListener_Table()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				focusRefType.setTableName(fTableText.getText());
				fEditor.refresh();
			}
		};
	}

	private ModifyListener createModifyListener_ID()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				focusRefType.setId(fIdText.getText());
				fEditor.refresh();
			}
		};
	}

	private ModifyListener createModifyListener_OID()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				focusRefType.setOid(fOidText.getText());
				fEditor.refresh();
			}
		};
	}

	private ModifyListener createModifyListener_Name()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				focusRefType.setName(fNameText.getText());
				fEditor.refresh();
			}
		};
	}

	private ModifyListener createModifyListener_Rev()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				focusRefType.setRevision(fRevText.getText());
				fEditor.refresh();
			}
		};
	}

	private ModifyListener createModifyListener_Vault()
	{
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e)
			{
				focusRefType.setVault(fVaultText.getText());
				fEditor.refresh();
			}
		};
	}

	@Override
	protected void updateFields()
	{
		focusRefType = (ReferenceType) fFocusObject;

		fTypeText.setText(focusRefType.getTypeName());
		fTableText.setText(focusRefType.getTableName());
		fIdText.setText(focusRefType.getId());
		fOidText.setText(focusRefType.getOid());
		fNameText.setText(focusRefType.getName());
		fRevText.setText(focusRefType.getRevision());
		fVaultText.setText(focusRefType.getVault());
	}

}
